/**
 * 
 */
package cn.ad.server.web.servlet.v2.requesthandler;

import cn.ad.server.common.utils.BrowserUtils;
import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.web.log.entity.AdActiveEventLog;
import cn.ad.server.web.log.entity.AdTrackingEventLog;
import cn.ad.server.web.servlet.v2.parser.AvidRequestParser;
import cn.ad.server.web.spring.model.AdRequest;
import cn.ad.server.web.spring.model.AvidRequest;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.dactive.DactiveAdResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;

/**
 * api回调
 * @author wangwei
 */
public class AvidRequestHandler extends DactiveRequestHandler{
	
	private static Logger LOGGER = LoggerFactory.getLogger(AvidRequestHandler.class);



	/**
	 * 
	 */
	public AvidRequestHandler() {
		setParser(new AvidRequestParser());
	}
	
	/* (non-Javadoc)
	 * @see cn.ad.server.web.servlet.v2.AdserverRequestServlet#handleExceptionResponse(java.lang.Exception, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void handleExceptionResponse(Exception e, HttpServletResponse response) {
		if(!(e instanceof NullResponseException)){
			LOGGER.error("处理AvidRequest 时出现异常，异常原因:{}", e.getMessage(), e);
		}
		response.setStatus(204);
		response.setContentLength(0);
	}

	@Override
	public AdActiveEventLog populate(AdRequest req, DactiveAdResponse res) {
		if(!(req instanceof AvidRequest)){
			return null;
		}
		AvidRequest request = (AvidRequest)req;
		AdActiveEventLog eventLog = new AdActiveEventLog();

		eventLog.setEventType(AdTrackingEventLog.MOBILEAPP_ACTIVE);
		eventLog.setRequestTimestamp(request.getRequestTimestamp());
		eventLog.setServerId(IPUtils.getHostName());
		eventLog.setGuid("");//TODO ....
		eventLog.setExceptionType(res.getStatus());
		eventLog.setRequestIp(request.getIp());
		eventLog.setAdspaceId(0);
		eventLog.setAdFormat(0);
		eventLog.setCreativeHeight(0);
		eventLog.setCreativeWidth(0);
		eventLog.setUa(request.getUa());
		if(StringUtils.isEmpty(request.getClient_ip())){
			eventLog.setIp(request.getIp());
		}else{
			eventLog.setIp(request.getClient_ip());
		}
		eventLog.setMuniqueType(res.getMuniqueType());
		eventLog.setMuniqueId(res.getMuniqueId());
		eventLog.setMobOps("");
		eventLog.setBrand("");
		eventLog.setModel("");
//		eventLog.setOs(request.getOs());
		eventLog.setConnType(0);
		eventLog.setDeviceType(0);
		eventLog.setScreenHeight(0);
		eventLog.setScreenWidth(0);
		eventLog.setLatitude("");
		eventLog.setLongitude("");
		eventLog.setCountryId(0);
		eventLog.setStateId(0);
		eventLog.setCityId(0);
		eventLog.setAreaId(0);
		eventLog.setBrowser(BrowserUtils.getBrowser(request.getUa()));
		eventLog.setCampaignId(0);
		eventLog.setStateId(0);
		eventLog.setCreativeId(0);
		eventLog.setTemplateId(0);
		eventLog.setMaterialId(0);
		eventLog.setIncome(0);
		eventLog.setPayment(0);
		eventLog.setImei(request.getImeiori());
		eventLog.setMac(request.getMacori());
		eventLog.setMac1(null);
		eventLog.setIdfa(request.getIdfaori());
//		eventLog.setAaid(request.getAdvertising_id());
//		eventLog.setAnid(request.getAndroid_id());
		eventLog.setUdid("");
		eventLog.setDuid("");
		eventLog.setImeisha1(null);
		eventLog.setDpidsha1(null);
		eventLog.setMacsha1(null);
		eventLog.setImeimd5(request.getImei());
		eventLog.setDpidmd5(request.getIdfa());
		eventLog.setMacmd5(request.getMac());
		eventLog.setOpenUdid(null);
		eventLog.setCustomId(null);
		eventLog.setAdxType(0);
		eventLog.setChannel_id(request.getChannelID());
		
		
		//--------------
		eventLog.setBchid(res.getFillChannelId());
		eventLog.setAvid(request.getAvid());
		
		return eventLog;
	}
	
	@Override
	public void validRequest(IAdserverRequest request) throws RequestParamValidFailedException {
		super.validRequest(request);
		
		AvidRequest req = (AvidRequest)request;
		if(StringUtils.isEmpty(req.getIdfa())&&StringUtils.isEmpty(req.getMac())&&StringUtils.isEmpty(req.getImei())){
			throw new RequestParamValidFailedException("no valid deviceid");
		}
	}
	
	
}
